//
//  create_enc_service_reply.hpp
// ~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2013-2014 Razvan Coca (razvan dot coca at  gmail dot com)
// This is mostly generated code. Generator is distributed under GPL. Edit it to suit your needs.
// 


#ifndef create_enc_service_reply_hpp
#define create_enc_service_reply_hpp
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>

#include "patterns/command.hpp"
#include "patterns/handlers.hpp"

#include "logging/logging.hpp"

namespace pcmd=patterns::command;
namespace phdl=patterns::handlers;
namespace ba=boost::asio;
namespace bs=boost::system;
namespace bt=boost::posix_time;

#include "components/defs/util_types.hpp"
#include "components/defs/server_processor.hpp"
#include "components/encrypt/aes_service.hpp"
namespace components{
namespace protocol{



class create_enc_service_reply :public boost::enable_shared_from_this<create_enc_service_reply>, private boost::noncopyable
{
private:
    std::string name_;

    ba::io_service& ios_;

    crypto_buf_t& iv_;

    crypto_buf_t& key_;

    stats_message_server_processor& message_processor_;

    std::string encrypt_buf_;

    create_enc_service_reply (ba::io_service& ios, crypto_buf_t& iv, crypto_buf_t& key, stats_message_server_processor& message_processor) ;
    boost::shared_ptr<components::encrypt::aes_service> aes_service_;
public:
    typedef boost::shared_ptr<create_enc_service_reply> pointer;

    bool cancel () ;

    static boost::shared_ptr<create_enc_service_reply> create (ba::io_service& ios, crypto_buf_t& iv, crypto_buf_t& key, stats_message_server_processor& message_processor) ;

    virtual  ~create_enc_service_reply () ;

    template<typename Handler >
    bool exec(std::string& send_buf, Handler& handler) {
        try{
            LOG(INFO)<<name_<<"::"<<__FUNCTION__;
            DLOG(INFO)<<"send_buf:\t"<<send_buf ;
            stats_message_t reply_message;
            udp_endpoint_t peer;
            bs::error_code ec;
            boost::logic::tribool protocol_response=message_processor_(reply_message,peer,ec);
            if(boost::logic::indeterminate(protocol_response))
                throw std::logic_error(" composing reply failed ");

            LOG(INFO)<<__FUNCTION__<<" "<<reply_message.data().name()<<":"<<reply_message.data().value().value();
            //if(reply_message.type()==stats_message_t::end)
                LOG(INFO)<<__FUNCTION__<<" transaction:"
                        <<reply_message.trans().trans_id()<<":"<<reply_message.trans().seq_no();
            encrypt_buf_=reply_message.SerializeAsString();
            send_buf.clear();
            aes_service_->enc_key(iv_,key_);
            aes_service_->dec_key(iv_,key_);
            bool ret=aes_service_->exec(components::encrypt::aes_service::encrypt_op,encrypt_buf_,send_buf,handler);

            return ret;
        }
        catch(std::exception &e){
            LOG(WARNING)<<name_<<"::"<<__FUNCTION__<<":"<<e.what();
            cancel();
            return false;
        }
        
    }
};

}//end namespace protocol
}//end namespace components

#endif /*#ifdef create_enc_service_reply_hpp*/
